<?php
class autenticacao_model extends CI_Model{
    public function __construct() {
        parent::__construct();
    }
    
    public function isSession() {
        $us = $this->session->userdata('user_logado');
        if (empty ($us)) {
            return false;
        }else {
            return true;
        }
    }
    
    public function getUsuarioSession() {
        return $this->session->userdata('user_logado');
    }
    
     public function isAdmin() {
        $us = $this->session->userdata('user_logado');
        $q = $this->db->query('select * from tb_usuario where usu_codigo ="'.$us['usu_codigo'].'" and usu_admin=1');
        if($q->num_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    public function isLogged() {
        if (!Current_User::user()) {
            redirect(base_url());
        }
    }
    
    public function logOut(){
        $this->session->sess_destroy();
    }


    public function autenticar($login,$senha){
        $q = $this->db->query('select * from tb_usuario where usu_login ="'.$login.'" and usu_senha="'.$senha.'"');
        $res  = $q->result();
        $cont = count($res);
        if($cont > 0){
            $CI = & get_instance();
            $CI->load->library('session');
            $usuario = array('usu_codigo'=>$res[0]->usu_codigo,'usu_nome'=>$res[0]->usu_nome);
            $CI->session->set_userdata('user_logado', $usuario);
            return true;
        }else{
            return false;
        }
    }
    
}

?>
